/**
 * Candidate number: 35834
 */
package tv.starfu.components.level;

import com.artemis.Component;
import tv.starfu.components.Transform;
import java.util.Random;

public class KyberneteGrid extends Component {
    
  //  private boolean isGenerated;
    public int width;
    public int length;
    public Compass[][] grid;    
    public Random noise;

    /**
     * Fills grid with paths and closes edges
     * @param width     grid width
     * @param length    grid length
     */
    public KyberneteGrid(int width, int length) {
        noise = new Random();
        this.width = width;
        this.length = length;
        grid = new Compass[width][length];
        System.out.println("KyberneteGrid has birthed itself");
        // Fill grid with random compasses
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < length; ++y) {       // traverse grid
                Compass c = new Compass();          // create new compass
                c.EAST  = noise.nextBoolean();      // randomise compass points
                c.WEST  = noise.nextBoolean();
                c.NORTH = noise.nextBoolean();
                c.SOUTH = noise.nextBoolean();
                
                // Close path at edges
                if(x == 0)       { c.WEST = false; }
                if(x == width-1) { c.EAST = false; }
                if(y == 0)       { c.NORTH = false; }
                
                // Put compass in grid
                grid[x][y] = c; }
        }
        
        createPaths();
        System.out.println("KyberneteGrid has paths, look: " + grid[1][3].WEST);
        System.out.println("Grid[1][3] : NORTH : " + grid[1][3].NORTH);
        System.out.println("Grid[1][3] : EAST : " + grid[1][3].EAST);
        System.out.println("Grid[1][3] : SOUTH : " + grid[1][3].SOUTH);
        System.out.println("Grid[1][3] : WEST : " + grid[1][3].WEST);
        
    }
  
    /**
     * arrange compass points in kybergrid to form traversable pathways.
     */
   public final void createPaths() {  
        for(int x = 0; x < width; x++) {
            for(int y = 0; y < length; ++y) {
                Compass c = grid[x][y];
  
                // Generate tile exits based on neighbours
                if(c.SOUTH && y < length-1) { grid[x][y+1].NORTH = true; }
                if(c.NORTH && y > 0 )       { grid[x][y-1].SOUTH = true; }
                if(c.EAST  && x < width-1)  { grid[x+1][y].WEST  = true; }
                if(c.WEST  && x > 0)        { grid[x-1][y].EAST  = true; } 
            }
        }
    }
         
}
